clear all

********************************************************************************
********************************************************************************
**** When Can We Trust Regression Discontinuity Design Estimates from Close ****
*************** Elections? Evidence from Experimental Benchmarks ***************
********************************************************************************
********************************************************************************

* This file produces Figure OA6 in Online Appendix D of De Magalhaes et al. 
* (Political Analysis).

cd "$dir"
use "$dir/data/personal_incumbency_advantage.dta", clear

********************************************************************************

cd "$dir/temp/"

est clear

// p = 1, MSE-optimal bandwidths

levelsof country, local(election)
		foreach x of local election {
		preserve
		forvalues i=-300(10)300 {
			local k = `i'/100
			rdrobust electednextelection rv if rv!=0 & country=="`x'", all rho(1) c(`k') vce(cluster constituency) kernel(uniform) bwselect(mserd)
			parmest, saving("fake`i'`x'.dta", replace)
			
			}

		forvalues i=-300(10)300 {

			use fake`i'`x'.dta, clear
			gen cutoff =`i'/100
			save, replace
			sleep 500
		}
		restore
		}
preserve	
	
use "$dir/data/personal_incumbency_advantage.dta", clear
	
levelsof country, local(election)
	foreach x of local election {
		use "fake-300`x'.dta", clear
		forvalues i=-290(10)300 {
		append using fake`i'`x'.dta
		}	

duplicates drop cutoff parm, force

twoway (rline min95 max95 cutoff if parm=="Conventional", plotregion(lcolor(black)) ylabel(,format(%9.2f)) lpattern(dash) lcolor(gray) xlabel(-3(1)3, format(%9.0f)) subtitle("Conventional, p = 1, MSE", position(11)) legend(off) xtitle("Cutoff") ytitle(Estimated effect)  graphregion(color(white))  xline(0, lcolor(cranberry)) yline(0, lcolor(black) lwidth(vthin))) (scatter estimate cutoff if parm=="Conventional", connect(direct) lcolor(black) msymbol(none))
graph save "$dir/temp/fakecutoff1_`x'1a.gph", replace
twoway (rline min95 max95 cutoff if parm=="Robust", plotregion(lcolor(black)) ylabel(,format(%9.2f)) lpattern(dash) lcolor(gray) xlabel(-3(1)3, format(%9.0f)) subtitle("Robust, p = 1, MSE", position(11)) legend(off) xtitle("Cutoff") ytitle(Estimated effect)  graphregion(color(white))  xline(0, lcolor(cranberry)) yline(0, lcolor(black) lwidth(vthin))) (scatter estimate cutoff if parm=="Robust", connect(direct) lcolor(black) msymbol(none))
graph save "$dir/temp/fakecutoff2_`x'1a.gph", replace
	}
restore

est clear

// p = 1, CER-optimal bandwidths

levelsof country, local(election)
		foreach x of local election {
		preserve
		forvalues i=-300(10)300 {
			local k = `i'/100
			rdrobust electednextelection rv if rv!=0 & country=="`x'", all rho(1) c(`k') vce(cluster constituency) kernel(uniform) bwselect(cerrd)
			parmest, saving("fake`i'`x'.dta", replace)
			
			}

		forvalues i=-300(10)300 {

			use fake`i'`x'.dta, clear
			gen cutoff =`i'/100
			save, replace
			sleep 500
		}
		restore
	}

preserve	
	
use "$dir/data/personal_incumbency_advantage.dta", clear
	
levelsof country, local(election)
	foreach x of local election {
		use "fake-300`x'.dta", clear
		forvalues i=-290(10)300 {
		append using fake`i'`x'.dta
		}	

duplicates drop cutoff parm, force

twoway (rline min95 max95 cutoff if parm=="Conventional", plotregion(lcolor(black)) ylabel(,format(%9.2f)) lpattern(dash) lcolor(gray) xlabel(-3(1)3, format(%9.0f)) subtitle("Conventional, p = 1, CER", position(11)) legend(off) xtitle("Cutoff") ytitle(Estimated effect)  graphregion(color(white))  xline(0, lcolor(cranberry)) yline(0, lcolor(black) lwidth(vthin))) (scatter estimate cutoff if parm=="Conventional", connect(direct) lcolor(black) msymbol(none))
graph save "$dir/temp/fakecutoff1_`x'1b.gph", replace
twoway (rline min95 max95 cutoff if parm=="Robust", plotregion(lcolor(black)) ylabel(,format(%9.2f)) lpattern(dash) lcolor(gray) xlabel(-3(1)3, format(%9.0f)) subtitle("Robust, p = 1, CER", position(11)) legend(off) xtitle("Cutoff") ytitle(Estimated effect)  graphregion(color(white))  xline(0, lcolor(cranberry)) yline(0, lcolor(black) lwidth(vthin))) (scatter estimate cutoff if parm=="Robust", connect(direct) lcolor(black) msymbol(none))
graph save "$dir/temp/fakecutoff2_`x'1b.gph", replace
	}
restore

est clear

// p = 2, MSE-optimal bandwidths

levelsof country, local(election)
		foreach x of local election {
		preserve
		forvalues i=-300(10)300 {
			local k = `i'/100
			rdrobust electednextelection rv if rv!=0 & country=="`x'", all p(2) rho(1) c(`k') vce(cluster constituency) kernel(uniform) bwselect(mserd)
			parmest, saving("fake`i'`x'.dta", replace)
			
			}

		forvalues i=-300(10)300 {

			use fake`i'`x'.dta, clear
			gen cutoff =`i'/100
			save, replace
			sleep 500
		}
		restore
	}

preserve	
	
use "$dir/data/personal_incumbency_advantage.dta", clear
	
levelsof country, local(election)
	foreach x of local election {
		use "fake-300`x'.dta", clear
		forvalues i=-290(10)300 {
		append using fake`i'`x'.dta
		}	

duplicates drop cutoff parm, force

twoway (rline min95 max95 cutoff if parm=="Conventional", plotregion(lcolor(black)) ylabel(,format(%9.2f)) lpattern(dash) lcolor(gray) xlabel(-3(1)3, format(%9.0f)) subtitle("Conventional, p = 2, MSE", position(11)) legend(off) xtitle("Cutoff") ytitle(Estimated effect)  graphregion(color(white))  xline(0, lcolor(cranberry)) yline(0, lcolor(black) lwidth(vthin))) (scatter estimate cutoff if parm=="Conventional", connect(direct) lcolor(black) msymbol(none))
graph save "$dir/temp/fakecutoff1_`x'2a.gph", replace
twoway (rline min95 max95 cutoff if parm=="Robust", plotregion(lcolor(black)) ylabel(,format(%9.2f)) lpattern(dash) lcolor(gray) xlabel(-3(1)3, format(%9.0f)) subtitle("Robust, p = 2, MSE", position(11)) legend(off) xtitle("Cutoff") ytitle(Estimated effect)  graphregion(color(white))  xline(0, lcolor(cranberry)) yline(0, lcolor(black) lwidth(vthin))) (scatter estimate cutoff if parm=="Robust", connect(direct) lcolor(black) msymbol(none))
graph save "$dir/temp/fakecutoff2_`x'2a.gph", replace
	}
restore

est clear

// p = 2, CER-optimal bandwidths

levelsof country, local(election)
		foreach x of local election {
		preserve
		forvalues i=-300(10)300 {
			local k = `i'/100
			rdrobust electednextelection rv if rv!=0 & country=="`x'", all p(2) rho(1) c(`k') vce(cluster constituency) kernel(uniform) bwselect(cerrd)
			parmest, saving("fake`i'`x'.dta", replace)
			
			}

		forvalues i=-300(10)300 {

			use fake`i'`x'.dta, clear
			gen cutoff =`i'/100
			save, replace
			sleep 500
		}
		restore
	}

preserve	
	
use "$dir/data/personal_incumbency_advantage.dta", clear
	
levelsof country, local(election)
	foreach x of local election {
		use "fake-300`x'.dta", clear
		forvalues i=-290(10)300 {
		append using fake`i'`x'.dta
		}	

duplicates drop cutoff parm, force

twoway (rline min95 max95 cutoff if parm=="Conventional", plotregion(lcolor(black)) ylabel(,format(%9.2f)) lpattern(dash) lcolor(gray) xlabel(-3(1)3, format(%9.0f)) subtitle("Conventional, p = 2, CER", position(11)) legend(off) xtitle("Cutoff") ytitle(Estimated effect)  graphregion(color(white))  xline(0, lcolor(cranberry)) yline(0, lcolor(black) lwidth(vthin))) (scatter estimate cutoff if parm=="Conventional", connect(direct) lcolor(black) msymbol(none))
graph save "$dir/temp/fakecutoff1_`x'2b.gph", replace
twoway (rline min95 max95 cutoff if parm=="Robust", plotregion(lcolor(black)) ylabel(,format(%9.2f)) lpattern(dash) lcolor(gray) xlabel(-3(1)3, format(%9.0f)) subtitle("Robust, p = 2, CER", position(11)) legend(off) xtitle("Cutoff") ytitle(Estimated effect)  graphregion(color(white))  xline(0, lcolor(cranberry)) yline(0, lcolor(black) lwidth(vthin))) (scatter estimate cutoff if parm=="Robust", connect(direct) lcolor(black) msymbol(none))
graph save "$dir/temp/fakecutoff2_`x'2b.gph", replace
	}
restore

// Combine the graphs

graph combine "$dir/temp/fakecutoff1_Brazil1a.gph" "$dir/temp/fakecutoff1_Brazil1b.gph" ///
	"$dir/temp/fakecutoff1_Brazil2a.gph" "$dir/temp/fakecutoff1_Brazil2b.gph" ///
	"$dir/temp/fakecutoff2_Brazil1a.gph" "$dir/temp/fakecutoff2_Brazil1b.gph" ///
	"$dir/temp/fakecutoff2_Brazil2a.gph" "$dir/temp/fakecutoff2_Brazil2b.gph", ///
	subtitle("Panel A: Brazil", position(11)) graphregion(color(white)) cols(4) xsize(16) ysize(9)
graph export "$dir/figures/figure_OA6A.pdf", as(pdf) replace

graph combine "$dir/temp/fakecutoff1_Colombia1a.gph" "$dir/temp/fakecutoff1_Colombia1b.gph" ///
	"$dir/temp/fakecutoff1_Colombia2a.gph" "$dir/temp/fakecutoff1_Colombia2b.gph" ///
	"$dir/temp/fakecutoff2_Colombia1a.gph" "$dir/temp/fakecutoff2_Colombia1b.gph" ///
	"$dir/temp/fakecutoff2_Colombia2a.gph" "$dir/temp/fakecutoff2_Colombia2b.gph", ///
	subtitle("Panel B: Colombia", position(11)) graphregion(color(white)) cols(4) xsize(16) ysize(9)
graph export "$dir/figures/figure_OA6B.pdf", as(pdf) replace

graph combine "$dir/temp/fakecutoff1_Denmark1a.gph" "$dir/temp/fakecutoff1_Denmark1b.gph" ///
	"$dir/temp/fakecutoff1_Denmark2a.gph" "$dir/temp/fakecutoff1_Denmark2b.gph" ///
	"$dir/temp/fakecutoff2_Denmark1a.gph" "$dir/temp/fakecutoff2_Denmark1b.gph" ///
	"$dir/temp/fakecutoff2_Denmark2a.gph" "$dir/temp/fakecutoff2_Denmark2b.gph", ///
	subtitle("Panel C: Denmark", position(11)) graphregion(color(white)) cols(4) xsize(16) ysize(9)
graph export "$dir/figures/figure_OA6C.pdf", as(pdf) replace

graph combine "$dir/temp/fakecutoff1_Finland1a.gph" "$dir/temp/fakecutoff1_Finland1b.gph" ///
	"$dir/temp/fakecutoff1_Finland2a.gph" "$dir/temp/fakecutoff1_Finland2b.gph" ///
	"$dir/temp/fakecutoff2_Finland1a.gph" "$dir/temp/fakecutoff2_Finland1b.gph" ///
	"$dir/temp/fakecutoff2_Finland2a.gph" "$dir/temp/fakecutoff2_Finland2b.gph", ///
	subtitle("Panel D: Finland", position(11)) graphregion(color(white)) cols(4) xsize(16) ysize(9)
graph export "$dir/figures/figure_OA6D.pdf", as(pdf) replace

est clear
